package com.example.config;

import com.example.enums.OrderStatus;
import com.example.enums.OrderStatusChangeEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

/**
 * @author: 21050134
 * @date: 2023/12/1 16:31
 * @description:
 */
@Configuration
@EnableStateMachine(name = "orderStateMachine")
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderStatus, OrderStatusChangeEvent> {

	/**
	 * 配置状态
	 *
	 * @param states
	 * @throws Exception
	 */
	@Override
	public void configure(StateMachineStateConfigurer<OrderStatus, OrderStatusChangeEvent> states) throws Exception {
		states
				.withStates()
				.initial(OrderStatus.WAIT_PAYMENT)
				.states(EnumSet.allOf(OrderStatus.class));
	}
	/**
	 * 配置状态转换事件关系
	 *
	 * @param transitions
	 * @throws Exception
	 */
	@Override
	public void configure(StateMachineTransitionConfigurer<OrderStatus, OrderStatusChangeEvent> transitions) throws Exception {
		transitions
				//支付事件:待支付-》待发货
				.withExternal().source(OrderStatus.WAIT_PAYMENT).target(OrderStatus.WAIT_DELIVER).event(OrderStatusChangeEvent.PAYED)
				.and()
				//发货事件:待发货-》待收货
				.withExternal().source(OrderStatus.WAIT_DELIVER).target(OrderStatus.WAIT_RECEIVE).event(OrderStatusChangeEvent.DELIVERY)
				.and()
				//收货事件:待收货-》已完成
				.withExternal().source(OrderStatus.WAIT_RECEIVE).target(OrderStatus.FINISH).event(OrderStatusChangeEvent.RECEIVED);
	}

}
